const {
  BrowserWindow,
  app,
  Tray,
  Menu,
  screen,
  protocol,
} = require("electron");
const path = require("path");
if (require("electron-squirrel-startup")) app.quit();
console.log(process.env.ELECTRON_URL);
// protocol.registerSchemesAsPrivileged([
//   { scheme: "app", privileges: { secure: true, standard: true } },
// ]);
let mainWindow = null;
app.setUserTasks([
  {
    program: process.execPath,
    arguments: "--new-window",
    iconPath: process.execPath,
    iconIndex: 0,
    title: "New Window",
    description: "Create a new window",
  },
]);

let win;
let quit = false;

function createTrayWindow() {
  const width = 300;
  const height = 420;
  win = new BrowserWindow({
    width,
    height,
    frame: false,
    resizable: false,
    show: false,
    movable: false,
    minimizable: false,
    maximizable: false,
    alwaysOnTop: true,
    skipTaskbar: true,
    // modal: true,
    // parent: mainWindow,
  });
  win.setSkipTaskbar(true);
  console.log(process.env);
  if (process.env.NODE_ENV == "development") {
    win.loadURL(`http://${process.env.ELECTRON_URL}`);
  } else {
    win.loadURL(`file://${__dirname}}/index.html`);
  }
}

const createWindow = () => {
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    //frame: false,
    titleBarStyle: "hidden",
    titleBarOverlay: {
      color: "#ffffff",
      symbolColor: "#74b1be",
    },
    webPreferences: {
      preload: path.join(__dirname, "../main/preload.js"),
    },
  });
  // mainWindow.setOverlayIcon("src/assets/logo.png", "Description for overlay");
  if (process.env.NODE_ENV == "development") {
    win.loadURL(`http://${process.env.ELECTRON_URL}`);
  } else {
    win.loadURL(`file://${__dirname}}/index.html`);
  }
  mainWindow.webContents.openDevTools();
  mainWindow.webContents.addListener("did-finish-load", () => {
    console.log("loading");
    // const child = new BrowserWindow({
    //     parent: mainWindow,
    //     modal: true,
    //     show: true,
    //     width: 500,
    //     height: 500,
    //     frame: false,
    // });
    // child.loadURL("https://github.com");
    // child.once("ready-to-show", () => {
    //     child.show();
    // });
  });
};

let tray = null;
let isLeave = true; // 存储鼠标是否离开托盘的状态
const checkTrayLeave = () => {
  clearInterval(this.leaveInter);
  this.leaveInter = setInterval(() => {
    let trayBounds = tray.getBounds();
    let point = screen.getCursorScreenPoint();
    // 判断是否再托盘内
    if (
      !(
        trayBounds.x < point.x &&
        trayBounds.y < point.y &&
        point.x < trayBounds.x + trayBounds.width &&
        point.y < trayBounds.y + trayBounds.height
      )
    ) {
      // 触发 mouse-leave
      clearInterval(this.leaveInter);
      win.hide(); // 隐藏菜单栏
      isLeave = true;
    } else {
      // console.log("isOn");
    }
  }, 100);
};
app.whenReady().then(() => {
  // tray = new Tray("dist/favicon.ico");
  // const contextMenu = Menu.buildFromTemplate([
  //   {
  //     label: "太蓝OA",
  //     click: function () {
  //       mainWindow.show();
  //     }, //打开相应页面
  //   },
  //   {
  //     label: "退出",
  //     click: function () {
  //       app.quit();
  //     },
  //   },
  // ]);
  // //tray.setToolTip("This is my application.");
  // tray.setContextMenu(contextMenu);

  // tray.on("mouse-move", () => {
  //   if (isLeave == true) {
  //     // 从其他地方第一次移入菜单时，开始显示菜单页，然后在菜单内移动时不重复再显示菜单
  //     win.show();
  //   }
  //   isLeave = false;
  //   checkTrayLeave();
  // });
  /**
   * 检查鼠标是否从托盘离开
   */

  //   let x = 0;
  //   tray.on("mouse-move", (e) => {
  //     console;
  //     const trayBounds = tray.getBounds();
  //     x = trayBounds.x;
  //     console.log(trayBounds);
  //     win.setPosition(
  //       trayBounds.x + trayBounds.width / 2 - 300 / 2,
  //       trayBounds.y - trayBounds.height - 420
  //     );

  //     win.show();
  //   });

  //   const timer = setInterval(() => {
  //     tray.getBounds().x !== x && (win.hide(), clearInterval(timer));
  //   }, 1000);
  //createTrayWindow(); //系统托盘
  createWindow(); //主窗口
  mainWindow.addListener("close", (e) => {
    if (mainWindow.isVisible && !quit) {
      console.log("关闭窗口");
      mainWindow.hide();
      e.preventDefault();
    }
  });
});

app.on("quit", () => {
  console.log("quit");
});

app.on("window-all-closed", () => {
  console.log("window-all-closed");
  app.quit();
});

app.on("before-quit", () => {
  quit = true;
  console.log("before-quit");
});

app.on("will-quit", () => {
  console.log("will-quit");
});
